package bankManagement;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class BankManagement {
	User loginUser;
	public ArrayList<User> encapsulation() throws IOException{
		ArrayList<User> list = new ArrayList<User>();
		File file = new File("user.txt");
		if(file != null){
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String str;
			while((str = br.readLine()) != null){
				String[] contents = str.split(":");
				User user = new User(contents[0],contents[1]);
				list.add(user);
			}
		}
		return list;
	}
	
	public boolean regist(String account, String password) throws IOException{
		User registUser = new User(account, password);
		if(encapsulation().contains(registUser)){
			return false;
		}
		FileWriter fw = new FileWriter("user.txt",true);
		BufferedWriter bw = new BufferedWriter(fw);
		bw.write((account+":"+password+"\r").toCharArray());
		bw.flush();
		bw.close();
		fw.close();
		return true;
	}
	
	public boolean login(String account, String password) throws IOException{
		User ifLoginUser = new User(account, password);
		for (User list : encapsulation()) {
			if(list.equals(ifLoginUser)){
				loginUser = list;
				return true;
			}
		}
		return false;
	}
	
	public boolean save(double money) throws IOException{
		if(money < 0){
			return false;
		}
		FileWriter fw = new FileWriter("save.txt",true);
		BufferedWriter bw = new BufferedWriter(fw);
		Date date = new Date();
		bw.write((loginUser.getAccount()+":save:"+money+":"+date.toLocaleString()+"\r").toCharArray());
		bw.flush();
		bw.close();
		fw.close();
		return true;
	}
	
	public boolean withDraw(double money) throws IOException{
		double count = 0;
		FileReader fr = new FileReader("save.txt");
		BufferedReader br = new BufferedReader(fr);
		String str;
		while((str = br.readLine()) != null){
			String[] contents = str.split(":");
			if(contents[0].equals(loginUser.getAccount())){
				count += Double.parseDouble(contents[2]);
			}
		}
		br.close();
		fr.close();
		if(count >= money){
			FileWriter fw = new FileWriter("withDraw.txt",true);
			BufferedWriter bw = new BufferedWriter(fw);
			Date date = new Date();
			bw.write((loginUser.getAccount()+":withdraw:"+money+":"+date.toLocaleString()+"\r").toCharArray());
			bw.flush();
			bw.close();
			fw.close();
			return true;
		}else{
			return false;
		}
	}
	
	public double selectBalance() throws IOException{
		double count = 0;
		FileReader fr = new FileReader("save.txt");
		BufferedReader br = new BufferedReader(fr);
		String str;
		while((str = br.readLine()) != null){
			String[] contents = str.split(":");
			if(contents[0].equals(loginUser.getAccount())){
				count += Double.parseDouble(contents[2]);
			}
		}
		br.close();
		fr.close();
		
		FileReader fr2 = new FileReader("withDraw.txt");
		BufferedReader br2 = new BufferedReader(fr2);
		while((str = br2.readLine()) != null){
			String[] contents2 = str.split(":");
			if(contents2[0].equals(loginUser.getAccount())){
				count -= Double.parseDouble(contents2[2]);
			}
		}
		br2.close();
		fr2.close();
		return count;
	}
	
	public ArrayList<String> selectSave() throws IOException{
		ArrayList<String> string = new ArrayList<String>();
		FileReader fr = new FileReader("save.txt");
		BufferedReader br = new BufferedReader(fr);
		String str;
		while((str = br.readLine()) != null){
			String[] contents = str.split(":");
			if(contents[0].equals(loginUser.getAccount())){
				string.add(str);
			}
		}
		br.close();
		fr.close();
		return string;
	}
	
	public ArrayList<String> selectWithDraw() throws IOException{
		ArrayList<String> string = new ArrayList<String>();
		FileReader fr = new FileReader("withDraw.txt");
		BufferedReader br = new BufferedReader(fr);
		String str;
		while((str = br.readLine()) != null){
			String[] contents = str.split(":");
			if(contents[0].equals(loginUser.getAccount())){
				string.add(str);
			}
		}
		br.close();
		fr.close();
		return string;
	}
	
	public ArrayList<String> selectByTime(String lowDate, String highDate) throws IOException, ParseException{
		ArrayList<String> string = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date lDate = sdf.parse(lowDate);
		Date hDate = sdf.parse(highDate);
		FileReader fr = new FileReader("save.txt");
		BufferedReader br = new BufferedReader(fr);
		String str;
		while((str = br.readLine()) != null){
			String[] contents = str.split(":");
			String temp = contents[3]+":"+contents[4]+":"+contents[5];
			Date date = sdf.parse(temp);
			if(contents[0].equals(loginUser.getAccount()) && date.before(hDate) && date.after(lDate)){
				string.add(str);
			}
		}
		br.close();
		fr.close();
		
		FileReader fr2 = new FileReader("withDraw.txt");
		BufferedReader br2 = new BufferedReader(fr2);
		while((str = br2.readLine()) != null){
			String[] contents = str.split(":");
			String temp = (contents[3]+":"+contents[4]+":"+contents[5]);
			Date date = sdf.parse(temp);
			if(contents[0].equals(loginUser.getAccount()) && date.before(hDate) && date.after(lDate)){
				string.add(str);
			}
		}
		br2.close();
		fr2.close();
		return string;
	}
}
